package gateway.server.route;

import gateway.api.engine.Filter;
import gateway.api.engine.Inbound;
import gateway.api.engine.Outbound;
import gateway.api.message.Message;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Jin Zheng
 * @since 1.0 2020-09-10
 */
public class Route implements Comparable<Route>
{
	private final String id;
	private RouteItem<Inbound> inbound;
	private List<RouteItem<Filter>> filterList = new ArrayList<>();
	private RouteItem<Outbound> outbound;
	private Map<String, String> args;
	private int order;

	public Route(String id)
	{
		this.id = id;
	}

	public boolean match(Message message)
	{
		if (inbound == null || inbound.getTarget() == null)
			return false;

		return inbound.getTarget().match(message);
	}

	public String getId()
	{
		return id;
	}

	public RouteItem<Inbound> getInbound()
	{
		return inbound;
	}

	public void setInbound(RouteItem<Inbound> inbound)
	{
		this.inbound = inbound;
	}

	public List<RouteItem<Filter>> getFilterList()
	{
		return filterList;
	}

	public void setFilterList(List<RouteItem<Filter>> filterList)
	{
		this.filterList = filterList;
	}

	public RouteItem<Outbound> getOutbound()
	{
		return outbound;
	}

	public void setOutbound(RouteItem<Outbound> outbound)
	{
		this.outbound = outbound;
	}

	public Map<String, String> getArgs()
	{
		return args;
	}

	public void setArgs(Map<String, String> args)
	{
		this.args = args;
	}

	public int getOrder()
	{
		return order;
	}

	public void setOrder(int order)
	{
		this.order = order;
	}

	@Override
	public boolean equals(Object o)
	{
		if (this == o)
		{
			return true;
		}
		if (o == null || getClass() != o.getClass())
		{
			return false;
		}
		Route route = (Route) o;
		return id.equals(route.id);
	}

	@Override
	public int hashCode()
	{
		return Objects.hash(id);
	}

	@Override
	public int compareTo(Route o)
	{
		return order - o.order;
	}
}
